<template>
    <view class='box'>
        <bc-title title="测试读取相册或拍摄"></bc-title>
		<view class="content">
			<view class="" @click="handleTest">
				我要读取相册
			</view>
		</view>
		<xyPermission :show="showTip" :title="titleTip" :content="contentTip"></xyPermission>
		<xySelectfileType :show="showSelectFile" @close="showSelectFile = false" @camera="handleCamera" @album="handleAlbum"></xySelectfileType>
    </view>
</template>

<script setup>
	import xyPermission from '@/uni_modules/xy-permission/components/xy-permission/xy-permission.vue'
	import xySelectfileType from '@/uni_modules/xy-permission/components/xy-permission/xy-selectfile-type.vue'
	import { requestAndroidPermission, gotoAppPermissionSetting } from '@/uni_modules/xy-permission/permission.js'
	
	const titleTip = ref('')
	const contentTip = ref('')
	const showTip = ref(false)
	const showSelectFile = ref(false)
	
	const handleTest = (() =>{
		showSelectFile.value = true
	})
	
	const handleCamera = (() =>{
		showSelectFile.value = false
		
		//#ifdef APP-PLUS
		titleTip.value = '摄像头权限申请说明'
		contentTip.value = '我们将获取系统摄像头功能，用于拍摄图片视频等发送给好友'
		const systemInfo = uni.getSystemInfoSync();
		if(systemInfo.platform === 'android'){
			showTip.value = true
			requestAndroidPermission('camera').then(res =>{
				if(res.data === 1){ //已有权限
					selectAlbum('camera') // 这里写你自己的逻辑代码
				}
			}).catch(err =>{
				uni.$u.toast(err.data);
			}).finally(() =>{
				showTip.value = false
			})
		}else{
			selectAlbum('camera')
		}
		//#endif
		
		//#ifdef H5
		selectAlbum('camera')
		//#endif
	})
	
	const handleAlbum = (() =>{
		showSelectFile.value = false
		
		//#ifdef APP-PLUS
		titleTip.value = '存储读取权限申请说明'
		contentTip.value = '我们将获取系统存储读取功能，用于选择本地图片视频等发送给好友'
		const systemInfo = uni.getSystemInfoSync();
		if(systemInfo.platform === 'android'){
			showTip.value = true
			requestAndroidPermission('storage').then(res =>{
				if(res.data === 1){ //已有权限
				    selectAlbum('album')
				}
			}).catch(err =>{
				uni.$u.toast(err.data);
			}).finally(() =>{
				showTip.value = false
			})
		}else{
			selectAlbum('album')
		}
		//#endif
		
		//#ifdef H5
		selectAlbum('album')
		//#endif
	})
	
	// 自己的逻辑代码示例
	const selectAlbum = ((type) =>{
		uni.chooseImage({
			sourceType: [type],
		    count: 9, // 默认9，设置图片的选择数量
		    sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
		    success: (res) => {
				console.log('拍摄或者相册选择图片上传');
		    },
		    fail: (err) => {
		        // 选择失败的回调
		        console.error(err);
		    }
		});
	})
</script>

<style>
	page{
		background-color: #f3f4f6;
	}
</style>
<style lang='scss' scoped>
    .box{
		.content{
			padding: 20rpx;
		}
	}
</style>